/**
 * 实名认证页面
 * Created by KY on 2017/8/9.
 */
define(['ldfw/core', 'text!tpl/authentication.tpl', 'weui', 'comps/session', 'comps/ajax', 'comps/validate', 'jquery-weui'],
    function (LDFW, tpl, weui, session, ajax, validate) {
        let View = LDFW.Container.extend({
            tpl: tpl,
            className: 'panel',
            events: {
                'change [name="cred_code"]': 'checkCredCode',
                'click a[name="submit"]': 'submit',
                'click a[name="logout"]': 'logout'
            },
            initialize: function () {
                this.on('before-shown',function(){
                    if (is_custom_skin && is_custom_skin.trim()) {
                        this.model.set('is_custom_skin', is_custom_skin)
                        this.model.set('co_no', co_no)
                    }
                    this.model.set('co_fixed_telephone',global_config.co_fixed_telephone)
                })
                this.on('shown', this.renderPage)
                if (is_custom_skin && is_custom_skin.trim()) {
                    this.model.set('is_custom_skin', is_custom_skin)
                    this.model.set('co_no', co_no)
                }
            },
            rendered: function () {
            },
            renderPage: function () {
                let user = session.getCurrentUser()
                this.model.set(user)
                this.render()
            },
            checkCredCode: function (event) {
                const self = this
                validate.checkElement($(event.target), 'id_card_code')
                    .then(function () {
                        $(event.target).removeClass('error')
                        self.enableSubmitCode()
                    }, function (msg) {
                        $.alert(msg, '错误')
                        $(event.target).addClass('error')
                        self.disableSubmitCode()
                    })
            },
            disableSubmitCode: function () {
                var $submitCodeBtn = this.$el.find('a[name="submit"]')
                $submitCodeBtn.addClass('weui-btn_disabled')
                $submitCodeBtn.attr('disabled', 'disabled')
            },
            enableSubmitCode: function () {
                var $submitCodeBtn = this.$el.find('a[name="submit"]')
                $submitCodeBtn.removeClass('weui-btn_disabled')
                $submitCodeBtn.removeAttr('disabled')
            },
            submit: function () {
                const _this = this
                weui.form.validate('#authForm', function (error) {
                    if (error) {
                        $(error.ele).addClass('error')
                        $(error.ele).one('blur', function () {
                            $(this).removeClass('error')
                        })
                    } else {
                        if (_this.model.get('cust_type') == '1') {
                            let cust_name = _this.$el.find('[name="cust_name"]').val(),
                                organ_cred_code = _this.$el.find('[name="organ_cred_code"]').val(),
                                organ = {
                                    cust_name: cust_name,
                                    organ_cred_code: organ_cred_code,
                                    organ_cred_type: 3,
                                    cust_type: 1
                                }
                            ajax.post('addOrgan.do', {organ: JSON.stringify(organ)})
                                .success(function (resp) {
                                    _this.handle(resp)
                                })
                        } else {
                            let cust_name = _this.$el.find('[name="cust_name"]').val(),
                                cred_code = _this.$el.find('[name="cred_code"]').val(),
                                person = {
                                    cust_name: cust_name,
                                    cred_code: cred_code,
                                    cred_type: 1,
                                    cust_type: 2
                                }
                            if (global_config.check_auth === 1) {
                                ajax.post('checkIdCardAuth.do', {
                                    realName: person.cust_name,
                                    cardCode: person.cred_code
                                })
                                    .success(function (resp) {
                                        if (!resp.resultCode) {
                                            ajax.post('addPerson.do', {person: JSON.stringify(person)})
                                                .success(function (resp) {
                                                    _this.handle(resp)
                                                })
                                        } else {
                                            $.alert('身份证校验失败')
                                        }
                                    })
                            } else {
                                ajax.post('addPerson.do', {person: JSON.stringify(person)})
                                    .success(function (resp) {
                                        _this.handle(resp)
                                    })
                            }
                        }
                    }
                })
            },
            handle: function (resp) {
                if (!resp.resultCode) {
                    session.flush()
                    LDFW.navigate('risk_assessment')
                } else {
                    $.alert('新增投资人失败')
                }
            },
            logout: function () {
                ajax.get('logout.do').success(function (resp) {
                    if (!resp.resultCode) {
                        session.clear();
                        LDFW.navigate('index')
                    }
                })
            }
        })
        return new View()
    })